﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleSeoApp
{
    public class WebHelper
    {
        public static string PostData(string url, string postData)
        {
            HttpWebRequest request = null;
            Uri uri = new Uri(url);
            request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postData.Length;
            using (Stream writeStream = request.GetRequestStream())
            {
                UTF8Encoding encoding = new UTF8Encoding();
                byte[] bytes = encoding.GetBytes(postData);
                writeStream.Write(bytes, 0, bytes.Length);
            }

            string result = string.Empty;
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
                    {
                        result = readStream.ReadToEnd();
                    }
                }
            }

            return result;
        }

        public static div DeSerialize(string XmlString)
        {
            div result = new div();
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(RemoveInvalidString(XmlString));
            XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
            XmlSerializer ser = new XmlSerializer(result.GetType());
            object obj = ser.Deserialize(reader);
            return (div)obj;
        }

        private static string RemoveInvalidString(string xmlstring)
        {
            return xmlstring.Replace("<br>", "").Replace(" & ", " &amp; ");
        }
    }
}
